<?php
/**
 * Created by PhpStorm.
 * User: HeiYanHeiMao
 * Date: 2020/2/28
 * Time: 13:24
 */

namespace app\model;


use think\facade\Db;

class OrderModel
{
    private static $baseTable = 'eb_order'; // 订单表
    private static $orderDetailTable = 'eb_order_detail'; // 订单详情表
    private static $storeTable = 'eb_store'; // 商户表
    private static $userTable = 'eb_user'; // 用户表
    private static $goodsTable = 'eb_goods'; // 商品表

    /**商户获取自己订单
     * @param $input
     * @param $token
     * @return \think\response\Json
     */
    public static function storeGetList($input, $token)
    {
        try {
            // 判断商户是否有效
            $redisConf = config('tools.redis');
            $redis = new \Redis();
            $redis->connect($redisConf['host'], $redisConf['port']);
            $storeId = $redis->get($token);
            if (!$storeId) return jsonFail('获取商户信息失败');
            $storeStatus = Db::table(self::$storeTable)->where('id', $storeId)->value('status');
            if ($storeStatus === null) return jsonFail('该商户不存在');
            // 0 未审核 1已审核 2审核未通过 3已禁用
            if ($storeStatus == 0) return jsonFail('该商户未审核,无法获取列表');
            if ($storeStatus == 2) return jsonFail('该商户审核未通过,无法获取列表');
            if ($storeStatus == 3) return jsonFail('该商户已禁用,无法获取列表');
            // 获取总页数
            $count = Db::table(self::$baseTable)->where('store_id', $storeId)->count();
            if ($count == 0) return jsonSuccess('获取成功', ['count' => 0, 'list' => []]);
            $orderInfo = Db::table(self::$baseTable)->alias('o')
                ->leftJoin('eb_user b', 'b.send_id = o.id')
                ->order('o.create_time', 'desc')
                ->where('o.store_id',$storeId)
                ->where('o.is_pay',1)
                ->field('b.name as send_name,b.phone as send_phone,o.dormitory_id,o.consignee_name,o.consignee_phone,o.consignee_address,o.create_time,o.back_reason,t.name as dormitory_name,s.name as school_name,o.remark,o.status,o.code,o.id,o.order_no,o.total_price,o.goods_price,o.delivery_price')->where('store_id', $storeId)->page($input['page'], $input['limit'])->select()->toArray();
            $orderIds = array_column($orderInfo, 'id');
            // 获取附属信息
            $otherInfos = Db::table(self::$orderDetailTable)->alias('od')
                ->leftJoin([self::$goodsTable => 'g'], 'od.goods_id = g.id')
                ->field('od.order_id,od.goods_num,od.goods_price,g.name')
                ->where('order_id', 'in', $orderIds)->select()->toArray();
            // 数据整合
            foreach ($otherInfos as $otherInfo) {
                $key = array_search($otherInfo['order_id'], $orderIds);
                $orderInfo[$key]['children'][] = $otherInfo;
                $orderInfo[$key]['completion_amount'] = Db::table('eb_order')->where('store_id',$storeId)->where('status','>=',5)->sum('store_price');
                $orderInfo[$key]['payment_amount'] = Db::table('eb_order')->where('store_id',$storeId)->where('operate_status',1)->sum('store_price');
                $orderInfo[$key]['completion_amount'] = Db::table('eb_order')->where('store_id',$storeId)->where('operate_status',0)->sum('total_price');
            }
            return jsonSuccess('获取成功', ['count' => ceil($count/$input['limit']), 'list' => $orderInfo]);
        } catch (\Exception $e) {
            return jsonFail('服务内部错误~');
        }
    }
    /**根据状态商户获取自己订单
     * @param $input
     * @param $token
     * @return \think\response\Json
     */
    public static function statusGetList($input, $token)
    {
        try {
            // 判断商户是否有效
            $redisConf = config('tools.redis');
            $redis = new \Redis();
            $redis->connect($redisConf['host'], $redisConf['port']);
            $storeId = $redis->get($token);
            if (!$storeId) return jsonFail('获取商户信息失败');
            $storeStatus = Db::table(self::$storeTable)->where('id', $storeId)->value('status');
            if ($storeStatus === null) return jsonFail('该商户不存在');
            // 0 未审核 1已审核 2审核未通过 3已禁用
            if ($storeStatus == 0) return jsonFail('该商户未审核,无法获取列表');
            if ($storeStatus == 2) return jsonFail('该商户审核未通过,无法获取列表');
            if ($storeStatus == 3) return jsonFail('该商户已禁用,无法获取列表');
            // 获取总页数
            if($input['status']== -3){
                $count = Db::table(self::$baseTable)->where('is_pay',1)->where('is_refues',1)->where('store_id', $storeId)->count();
                $orderInfo = Db::table(self::$baseTable)->alias('o')
                    ->leftJoin('eb_school s', 'o.school_id = s.id')
                    ->leftJoin('eb_dormitory t', 'o.dormitory_id = t.id')
                    ->leftJoin('eb_user b', 'o.send_id = b.id')
                    ->where('o.store_id',$storeId)
                    ->where('o.is_pay',1)
                    ->order('o.create_time', 'desc')
                    ->where('o.is_refues',1)
                    ->field('b.name as send_name,b.phone as send_phone,o.store_price,o.dormitory_id,o.consignee_name,o.consignee_phone,o.consignee_address,o.create_time,o.back_reason,t.name as dormitory_name,s.name as school_name,o.remark,o.status,o.code,o.id,o.order_no,o.total_price,o.goods_price,o.delivery_price')->where('store_id', $storeId)->page($input['page'], $input['limit'])->select()->toArray();
            }else{
                $count = Db::table(self::$baseTable)->where('is_pay',1)->where('status',$input['status'])->where('store_id', $storeId)->count();
                if ($count == 0) return jsonSuccess('获取成功', ['count' => 0, 'list' => []]);
                $orderInfo = Db::table(self::$baseTable)->alias('o')
                    ->leftJoin('eb_school s', 'o.school_id = s.id')
                    ->leftJoin('eb_dormitory t', 'o.dormitory_id = t.id')
                    ->leftJoin('eb_user b', 'o.send_id = b.id')
                    ->where('o.store_id',$storeId)
                    ->where('o.is_pay',1)
                    ->order('o.create_time', 'desc')
                    ->where('o.status',$input['status'])
                    ->field('b.name as send_name,b.phone as send_phone,o.store_price,o.dormitory_id,o.consignee_name,o.consignee_phone,o.consignee_address,o.create_time,o.back_reason,t.name as dormitory_name,s.name as school_name,o.remark,o.status,o.code,o.id,o.order_no,o.total_price,o.goods_price,o.delivery_price')->where('store_id', $storeId)->page($input['page'], $input['limit'])->select()->toArray();
            }
            $orderIds = array_column($orderInfo, 'id');
            // 获取附属信息
            $otherInfos = Db::table(self::$orderDetailTable)->alias('od')
                ->leftJoin([self::$goodsTable => 'g'], 'od.goods_id = g.id')
                ->field('od.order_id,od.goods_num,od.goods_price,g.name')
                ->where('order_id', 'in', $orderIds)->select()->toArray();
            // 数据整合
            foreach ($otherInfos as $otherInfo) {
                $key = array_search($otherInfo['order_id'], $orderIds);
                $orderInfo[$key]['children'][] = $otherInfo;
            }
            return jsonSuccess('获取成功', ['count' => ceil($count/$input['limit']), 'list' => $orderInfo]);
        } catch (\Exception $e) {
            return jsonFail($e->getMessage());
        }
    }
}